public class GCDemo {
    private static final int OBJECT_SIZE = 100_000;
    private static final int LOOP_COUNT = 1000;
    
    static class GCObject {
        byte[] bytes = new byte[OBJECT_SIZE];
    }
    
    public static void main(String[] args) throws InterruptedException {
        while (true) {
            for (int i = 0; i < LOOP_COUNT; i++) {
                GCObject obj = new GCObject();
            }

            // 手动请求进行垃圾回收
            // System.gc();

            // 输出当前的内存情况
            Runtime runtime = Runtime.getRuntime();
            long totalMemory = runtime.totalMemory();
            long freeMemory = runtime.freeMemory();
            long usedMemory = totalMemory - freeMemory;
            System.out.println("总内存: " + totalMemory + "，已使用内存: " + usedMemory + "，空闲内存: " + freeMemory);

            // 等待一段时间，让我们有机会观察垃圾回收器的行为
            Thread.sleep(200);
        }
    }
}
